<template>
  <j-modal :title="title" :width="1300" :visible="visible" switchFullscreen @cancel="handleCancel" :footer="null">
    <a-spin :spinning="loading">
      <div class="content">
        <a-card title="考核计划配置" v-if="componentName === 'issued'">
          <a-form-model ref="form" :model="form" :rules="rules" :label-col="{ span: 7 }" :wrapper-col="{ span: 16 }">
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="计划编号" prop="planCode">
                  <a-input v-model="form.planCode" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="计划名称" prop="planName">
                  <a-input v-model="form.planName" :readOnly="isDisabled" :placeholder="compPlaceholderInput">
                  </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="考核类型" prop="planType">
                  <a-input
                    v-if="isDisabled"
                    :value="$filterDictTextByCache('examinType', form.planType)"
                    readOnly
                  ></a-input>
                  <a-select
                    v-else
                    v-model="form.planType"
                    :placeholder="compPlaceholderSel"
                    style="width: 100%"
                    allowClear
                  >
                    <a-select-option v-for="(item, key) in assTypeOpts" :key="key" :value="item.value"
                      >{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="考核周期" prop="timeRange">
                  <a-input v-if="isDisabled" :value="`${form.startTime || '-'} ~ ${form.endTime || '-'}`" readOnly>
                  </a-input>
                  <a-range-picker
                    v-else
                    v-model="form.timeRange"
                    style="width: 100%"
                    format="YYYY-MM-DD"
                    @change="onTimeChange"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="年" prop="year">
                  <a-input v-if="isDisabled" :value="form.year" readOnly> </a-input>
                  <a-date-picker
                    v-else
                    mode="year"
                    v-model="form.year"
                    :placeholder="compPlaceholderInput"
                    format="YYYY"
                    style="width: 100%"
                    :open="openYear"
                    @openChange="openChangeOne"
                    @panelChange="panelChangeOne"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="季度" prop="quarter">
                  <a-input v-if="isDisabled" :value="form.quarter" readOnly> </a-input>
                  <a-select
                    v-else
                    v-model="form.quarter"
                    :placeholder="compPlaceholderSel"
                    style="width: 100%"
                    allowClear
                  >
                    <a-select-option v-for="(item, key) in quarterOpts" :key="key" :value="item.value"
                      >{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="月" prop="month">
                  <a-input v-if="isDisabled" :value="form.month" readOnly> </a-input>
                  <a-select
                    v-else
                    v-model="form.month"
                    :placeholder="compPlaceholderSel"
                    style="width: 100%"
                    allowClear
                  >
                    <a-select-option v-for="(item, key) in monthOpts" :key="key" :value="item.value"
                      >{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row v-if="!isDisabled">
              <a-col :span="6">
                <a-form-model-item label="方案名称" prop="schemeName">
                  <a-input v-model="form.schemeName" readOnly placeholder="请选择"> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="">
                  <a-button class="btn" @click="$refs.selPlan.show()">选 择</a-button>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-card>
        <a-card title="基本信息" v-if="['distribute', 'signIn', 'assign'].includes(componentName)">
          <a-form-model ref="form" :model="form" :rules="rules" :label-col="{ span: 7 }" :wrapper-col="{ span: 16 }">
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="计划编号" prop="planCode">
                  <a-input v-model="form.planCode" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="计划名称" prop="planName">
                  <a-input v-model="form.planName" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="考核类型" prop="planType">
                  <a-input :value="$filterDictTextByCache('examinType', form.planType)" readOnly></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="年/季/月">
                  <a-input :value="`${form.year || '-'}/${form.quarter || '-'}/${form.month || '-'}`" readOnly>
                  </a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="考核周期" prop="timeRange">
                  <a-input :value="`${form.startTime || '-'} ~ ${form.endTime || '-'}`" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="分发单位" prop="separateName">
                  <a-input v-model="form.separateName" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="签收状态" prop="signStatus">
                  <a-input :value="$filterDictTextByCache('signStatus', form.signStatus)" readOnly></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="创建时间" prop="separateTime">
                  <a-input v-model="form.separateTime" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="下发单位" prop="downName">
                  <a-input v-model="form.downName" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="下发状态" prop="downStatus">
                  <a-input :value="$filterDictTextByCache('downStatus', form.downStatus)" readOnly></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="下发时间" prop="downTime">
                  <a-input v-model="form.downTime" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-card>
        <a-card title="方案基本信息">
          <a-form-model :model="formByPlan" :rules="rules" :label-col="{ span: 7 }" :wrapper-col="{ span: 16 }">
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="方案编码" prop="schemeCode">
                  <a-input :value="formByPlan.schemeCode" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="方案名称" prop="schemeName">
                  <a-input :value="formByPlan.schemeName" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="适用范围" prop="scope">
                  <a-input :value="$filterDictTextByCache('appScope', formByPlan.scope)" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="是否有效" prop="status">
                  <a-input :value="$filterDictTextByCache('yn', formByPlan.status)" readOnly></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="6">
                <a-form-model-item label="总 分" prop="totalScore">
                  <a-input :value="formByPlan.totalScore" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="创建人" prop="createBy">
                  <a-input :value="formByPlan.createBy" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="6">
                <a-form-model-item label="创建时间" prop="createTime">
                  <a-input :value="formByPlan.createTime" readOnly> </a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
            <a-row>
              <a-col :span="11" class="col-3">
                <a-form-model-item label="方案说明" prop="remark">
                  <a-textarea :value="formByPlan.remark" readOnly :rows="3"></a-textarea>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </a-card>
        <a-card
          :title="['signIn', 'assign'].includes(componentName) ? '方案指标信息' : '数据采集审核配置'"
          class="index-card"
        >
          <template #extra v-if="componentName === 'assign'">
            <a-button class="btn" @click="handleImport" style="margin-left: 20px">导入</a-button>
            <a-button class="btn" @click="handleDownload" style="margin-left: 8px">下载模板</a-button>
          </template>
          <div class="index-con">
            <div class="index-left">
              <a-tree
                v-if="treeData.length"
                :checkable="!isDisabled"
                :tree-data="treeData"
                v-model:checkedKeys="checkedKeys"
                :defaultExpandAll="true"
              >
                <template #title="row"
                  ><span v-if="row.children && row.children.length > 0">{{ row.title }}</span
                  ><span v-else style="color: #1890ff" @click="indexDetail(row)">{{ row.title }}</span>
                </template>
              </a-tree>
            </div>
            <div class="index-right">
              <!-- 下发/分发 -->
              <a-form-model
                :model="form"
                :rules="rules"
                :label-col="{ span: 7 }"
                :wrapper-col="{ span: 16 }"
                v-if="['issued', 'distribute'].includes(componentName)"
              >
                <a-row>
                  <a-col :span="8">
                    <a-form-model-item label="审核机构" prop="auditOrgan">
                      <a-input v-if="isDisabled" :value="formByPlan.auditOrganName" readOnly> </a-input>
                      <a-select
                        v-else
                        v-model="formByPlan.auditOrgan"
                        :placeholder="compPlaceholderSel"
                        style="width: 100%"
                        allowClear
                        @change="changeOrgs"
                      >
                        <a-select-option v-for="(item, index) in auditOrgOpts" :key="index" :value="item.id"
                          >{{ item.departName }}
                        </a-select-option>
                      </a-select>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-model-item label="审核部门" prop="auditDepart">
                      <a-input v-if="isDisabled" :value="formByPlan.auditDepartName" readOnly> </a-input>
                      <a-select
                        v-else
                        v-model="formByPlan.auditDepart"
                        :placeholder="compPlaceholderSel"
                        style="width: 100%"
                        allowClear
                        @change="changeDepart"
                      >
                        <a-select-option v-for="item in auditDepOpts" :key="item.id"
                          >{{ item.departName }}
                        </a-select-option>
                      </a-select>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-form-model>
              <!-- 签收 -->
              <a-form-model
                :model="form"
                :rules="rules"
                :label-col="{ span: 8 }"
                :wrapper-col="{ span: 14 }"
                v-else-if="componentName === 'signIn'"
              >
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="指标名称" prop="indicatorName">
                      <a-input :value="curRow.indicatorName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="所属目录" prop="contentIndicator">
                      <a-input :valu="curRow.contentIndicator" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="指标类型" prop="indicatorType">
                      <a-input :value="$filterDictTextByCache('indicator', curRow.indicatorType)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="加/减分（P）" prop="addCardinalNumber">
                      <a-input :value="curRow.addCardinalNumber" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="基 数（Z）" prop="cardinalNumber">
                      <a-input :value="curRow.cardinalNumber" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="任务量（Y）" prop="taskNumber">
                      <a-input :value="curRow.taskNumber" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="上限分值" prop="upperScore">
                      <a-input :value="curRow.upperScore" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="下限分值" prop="lowerScore">
                      <a-input :value="curRow.lowerScore" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="是否权重" prop="pre">
                      <a-input :value="curRow.pre" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="权重%（M）" prop="weight">
                      <a-input :value="curRow.weight" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="是否与上级共享评分" prop="isSharedScore">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isSharedScore)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="是否可修正得分" prop="isScoreCorrection">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isScoreCorrection)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="是否禁用" prop="isDisable">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isDisable)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11" class="col-2">
                    <a-form-model-item label="考核标准" prop="standardAssessment">
                      <a-textarea :value="curRow.standardAssessment" readOnly :rows="3"></a-textarea>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-form-model>
              <!-- 指派 -->
              <a-form-model
                :model="form"
                :rules="rules"
                :label-col="{ span: 8 }"
                :wrapper-col="{ span: 14 }"
                v-else-if="componentName === 'assign'"
              >
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="指标名称" prop="indicatorName">
                      <a-input :value="curRow.indicatorName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="上级目录/上级指标" prop="seniorIndicator">
                      <a-input :value="curRow.seniorIndicator" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="指标类型" prop="indicatorType">
                      <a-input :value="$filterDictTextByCache('indicator', curRow.indicatorType)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="加/减分基数（P）" prop="addCardinalNumber">
                      <a-input :value="curRow.addCardinalNumber" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="任务量（Y）" prop="taskNumber">
                      <a-input v-model="curRow.taskNumber" placeholder="请输入"> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="基 数（Z）" prop="cardinalNumber">
                      <a-input :value="curRow.cardinalNumber" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="是否可修正得分" prop="isScoreCorrection">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isScoreCorrection)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="是否与上级共享评分" prop="isSharedScore">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isSharedScore)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="上限分值" prop="upperScore">
                      <a-input :value="curRow.upperScore" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="下限分值" prop="lowerScore">
                      <a-input :value="curRow.lowerScore" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="权重%（M）" prop="weight">
                      <a-input :value="curRow.weight" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="是否禁用" prop="isDisable">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isDisable)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="是否末级" prop="collectWay">
                      <a-input :value="$filterDictTextByCache('yn', curRow.collectWay)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="顺 序" prop="sort">
                      <a-input :value="curRow.sort" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="初评分值（N）" prop="firstScore">
                      <a-input :value="curRow.firstScore" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="完成量（L）" prop="finishNumber">
                      <a-input :value="curRow.finishNumber" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="是否允许分发" prop="isDistribution">
                      <a-input :value="$filterDictTextByCache('yn', curRow.isDistribution)" readOnly></a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="采集方式" prop="collectionMethod">
                      <a-input
                        :value="$filterDictTextByCache('collectionMethod', curRow.collectionMethod)"
                        readOnly
                      ></a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="审核机构" prop="auditOrganName">
                      <a-input :value="curRow.auditOrganName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="审核部门" prop="auditDepartName">
                      <a-input :value="curRow.auditDepartName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="审核人" prop="auditPeopleName">
                      <a-input :value="curRow.auditPeopleName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="采集机构" prop="collectOrganName">
                      <a-input :value="curRow.collectOrganName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="11">
                    <a-form-model-item label="采集部门" prop="collectDepartName">
                      <a-input :value="curRow.collectDepartName" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="11">
                    <a-form-model-item label="采集人" prop="collectPeople">
                      <a-input :value="curRow.collectPeople" readOnly> </a-input>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="10" class="col-2">
                    <a-form-model-item label="考核标准" prop="standardAssessment">
                      <a-textarea :value="curRow.standardAssessment" readOnly :rows="3"></a-textarea>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-form-model>
            </div>
          </div>
        </a-card>
        <a-card
          :title="`${compTitle}对象`"
          class="card-end"
          v-if="['issued', 'distribute', 'signIn'].includes(componentName)"
        >
          <a-form-model
            v-if="!isDisabled && this.componentName !== 'signIn'"
            :model="formByDisObj"
            :rules="rules"
            :label-col="{ span: 7 }"
            :wrapper-col="{ span: 16 }"
          >
            <a-row>
              <a-col :span="8">
                <a-form-model-item :label="`${compTitle}对象`" prop="name">
                  <a-input v-model="formByDisObj.name" readOnly placeholder="请选择"> </a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="">
                  <a-button class="btn" @click="$refs.selObj.show(`选择${compTitle}对象`)">选 择</a-button>
                  <a-button type="primary" icon="delete" class="ant-btn-danger" style="margin-left: 8px" @click="onDel"
                    >删 除</a-button
                  >
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
          <div class="table-page-content">
            <z-table
              ref="table"
              rowKey="id"
              :showAlert="false"
              :showSelection="!isDisabled"
              :showPagination="false"
              :columns="columns"
              :dataSource="pageData"
              @selectedRow="onSelectedRow"
            >
              <!--操作-->
              <template slot="action" slot-scope="text, record">
                <a-button @click="handleIndexInfo(record)" type="link" style="padding: 0">指标信息</a-button>
              </template>
            </z-table>
          </div>
        </a-card>
      </div>
    </a-spin>
    <div class="footerBox">
      <template v-if="!isDisabled">
        <a-button class="btn" @click="handleCancel">取消</a-button>
        <a-button
          v-if="['issued', 'distribute', 'assign'].includes(componentName)"
          class="btn"
          @click="handleOk(componentName)"
          style="margin-left: 8px"
          type="primary"
          >保存</a-button
        >
        <a-button
          v-if="componentName === 'signIn'"
          class="btn"
          @click="handleSign"
          style="margin-left: 8px"
          type="primary"
          >签收</a-button
        >
        <a-button
          v-if="componentName === 'assign'"
          class="btn"
          @click="handleSubmit"
          style="margin-left: 8px"
          type="primary"
          >提交</a-button
        >
        <a-button
          v-if="componentName === 'issued' && showType === '2'"
          class="btn"
          @click="handleIssued"
          style="margin-left: 8px"
          type="primary"
          >下发</a-button
        >
        <a-button
          v-if="componentName === 'distribute'"
          class="btn"
          @click="handleDistribute"
          style="margin-left: 8px"
          type="primary"
          >分发</a-button
        >
      </template>
    </div>
    <SelPlan ref="selPlan" @ok="selPlan" />
    <SelObj ref="selObj" @ok="selObj" />
    <PlanIndexInfo ref="planIndexInfo" @ok="selIndexInfo" />
  </j-modal>
</template>

<script>
import SelPlan from './selPlan'
import SelObj from './selObj'
import PlanIndexInfo from './planIndexInfo'

import Api from '@/api/sjsk/performanceService'
import { getDictItemsFromCache } from '@/api/api'
import moment from 'moment'

export default {
  name: 'addPlan',
  components: {
    SelPlan,
    SelObj,
    PlanIndexInfo,
  },
  props: {
    visible: Boolean,
    showType: String,
    checkRow: Object,
  },
  data() {
    return {
      moment,
      title: null,
      loading: false,
      form: {},
      formByPlan: {},
      formByDisObj: {},
      isDisabled: false,
      rules: {
        planName: [{ required: true, message: '请输入计划名称' }],
        planType: [{ required: true, message: '请选择考核类型' }],
        timeRange: [{ required: true, message: '请选择考核周期' }],
      },
      assTypeOpts: getDictItemsFromCache('examinType'),
      quarterOpts: getDictItemsFromCache('quarter'),
      monthOpts: getDictItemsFromCache('month'),
      ysOpts: getDictItemsFromCache('yn'),
      openYear: false,
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) => index + 1,
        },
        {
          title: '下发对象',
          dataIndex: 'title',
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={text} length={20} />
          },
        },
        {
          title: '签收状态',
          dataIndex: 'signStatus',
          align: 'center',
          customRender: (text) => {
            return <j-ellipsis value={this.$filterDictTextByCache('signStatus', text)} />
          },
        },
        {
          title: '签收时间',
          dataIndex: 'signTime',
          align: 'center',
        },
        {
          title: '操作',
          width: 160,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageData: {},
      componentName: null,
      checkIndexs: [],
      cloneIndexInfo: {},
      treeData: [],
      checkedKeys: [],
      auditOrgOpts: [],
      auditDepOpts: [],
      curRow: {},
      checkList: [],
      indexInfoCurRow: {},
    }
  },
  created() {
    this.componentName = this.$route.meta.componentName
    if (this.componentName === 'distribute') {
      let findData = this.columns.find((el) => el.dataIndex === 'title')
      findData.title = '分发对象'
    } else if (this.componentName === 'signIn') {
      this.columns = this.columns.filter((el) => el.dataIndex !== 'action')
      let findData = this.columns.find((el) => el.dataIndex === 'title')
      findData.title = '考核单位'
    }
    const type = this.showType
    if (type === '1') {
      this.title = `新增${this.compTitle}`
      if (this.componentName === 'distribute') {
        this.getDetail('findByIdAppraisalPlanSeparate')
      } else if (this.componentName === 'signIn') {
        this.getDetail('findByIdAppraisalPlanSign')
      }
    } else if (type === '2') {
      this.title = `编辑${this.compTitle}`
      if (this.componentName === 'issued') {
        this.getDetail('findByIdAppraisalPlanDown')
      } else if (this.componentName === 'assign') {
        this.title = `${this.compTitle}`
        this.getDetail('findByIdAppraisalPlanAssign')
      }
    } else if (type === '3') {
      this.title = `${this.compTitle}详情`
      if (this.componentName === 'issued') {
        this.getDetail('findByIdAppraisalPlanDown')
      } else if (this.componentName === 'distribute') {
        this.getDetail('findByIdAppraisalPlanSeparate')
      } else if (this.componentName === 'signIn') {
        this.getDetail('findByIdAppraisalPlanSign')
      }
    }
    this.isDisabled = type === '3'
  },
  mounted() {},

  computed: {
    compTitle() {
      let title = null
      switch (this.componentName) {
        case 'issued':
          title = '下发'
          break
        case 'distribute':
          title = '分发'
          break
        case 'signIn':
          title = '签收'
          break
        case 'assign':
          title = '指派'
          break
        default:
          break
      }
      return title
    },
    compPlaceholderInput() {
      return this.showType !== '3' ? '请输入' : ''
    },
    compPlaceholderSel() {
      return this.showType !== '3' ? '请选择' : ''
    },
  },
  watch: {
    // 任务量
    'curRow.taskNumber': {
      handler(newVal, oldVal) {
        if (newVal && newVal !== oldVal) {
          let row = this.checkIndexs.find((el) => el.gradeIndicator === this.curRow.gradeIndicator)
          row.taskNumber = newVal
        }
      },
      immediate: true,
      // deep: true
    },
  },

  methods: {
    // 获取下发详情
    getDetail(_apiName) {
      this.loading = true
      Api[_apiName]({ id: this.checkRow.id })
        .then((res) => {
          if (res.success) {
            let { sjskSchemeDto, listSjskAppraisalPlanSendDto, startTime, endTime, planType } = res.result
            this.form = res.result
            this.$set(this.form, 'timeRange', [this.moment(startTime), this.moment(endTime)])
            this.$set(this.form, 'planType', planType.toString())
            this.cloneIndexInfo = JSON.parse(JSON.stringify(sjskSchemeDto))
            this.formByPlan = sjskSchemeDto
            this.treeData = sjskSchemeDto.tree
            this.checkIndexs = sjskSchemeDto.listSjskTableIndicatorDto
            this.$set(this.pageData, 'records', listSjskAppraisalPlanSendDto)
            if (!['signIn', 'assign'].includes(this.componentName) && !this.isDisabled) {
              this.getAuditOrgs()
            }
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 获取审核机构
    async getAuditOrgs() {
      await Api.queryDepartList({ departType: 2, orgTypeExt: 2 }).then((res) => {
        if (res.success) {
          this.auditOrgOpts = res.result
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 获取审核部门
    async changeOrgs(_id) {
      let row = this.checkIndexs.find((el) => el.gradeIndicator === this.curRow.gradeIndicator)
      row.auditOrgan = _id
      this.auditDepOpts = []
      await Api.queryDepartList({ departType: 2, orgTypeExt: 1, parentDepartId: _id }).then((res) => {
        if (res.success) {
          this.formByPlan.auditDepart = null
          this.auditDepOpts = res.result
          if (this.auditDepOpts.length) {
            if (this.auditDepOpts.map((el) => el.id).includes(this.curRow.auditDepart)) {
              this.formByPlan.auditDepart = this.curRow.auditDepart
            }
          }
        } else {
          this.$message.error(res.message)
        }
      })
    },
    changeDepart(_id) {
      let row = this.checkIndexs.find((el) => el.gradeIndicator === this.curRow.gradeIndicator)
      row.auditDepart = _id
    },

    // 指标详情
    indexDetail(_item) {
      let row = this.checkIndexs.find((el) => el.gradeIndicator === _item.key)
      console.log('row--', row)
      this.curRow = row
      if (this.isDisabled) {
        this.$set(this.formByPlan, 'auditOrganName', row.auditOrganName)
        this.$set(this.formByPlan, 'auditDepartName', row.auditDepartName)
      } else {
        this.$set(this.formByPlan, 'auditOrgan', row.auditOrgan)
        this.changeOrgs(this.formByPlan.auditOrgan)
      }
    },

    // 方案详情
    getByIdScheme(_id) {
      this.loading = true
      Api.findByIdScheme({ id: _id })
        .then((res) => {
          if (res.success) {
            this.cloneIndexInfo = JSON.parse(JSON.stringify(res.result))
            let { listSjskTableIndicatorDto, tree } = res.result
            this.checkIndexs = listSjskTableIndicatorDto
            this.treeData = tree
            this.getAuditOrgs()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 删除
    onDel() {
      if (!this.checkList.length) {
        this.$message.warn('请选择数据')
        return
      }
      const that = this
      const ids = this.checkList.map((el) => el.id)
      this.$confirm({
        title: `确定是否删除?`,
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          that.$set(
            that.pageData,
            'records',
            that.pageData.records.filter((el) => !ids.includes(el.id))
          )
          that.$set(that.formByDisObj, 'name', that.pageData.records.map((el) => el.title).join(','))
          that.checkList = []
        },
      })
    },

    // 保存
    handleOk(_componentName) {
      const that = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (!that.checkIndexs.length) {
            that.$message.warn('暂无指标信息')
            return false
          }
          if (!(that.pageData && that.pageData.records && that.pageData.records.length)) {
            that.$message.warn(`暂无${this.compTitle}对象`)
            return false
          }
          that.loading = true
          let methodApi = null
          // 方案基本信息
          that.form.sjskSchemeDto = {
            ...this.formByPlan,
            listSjskTableIndicatorDto: that.checkIndexs, // 指标
          }
          // 考核计划下发、分发对象
          that.form.listSjskAppraisalPlanSendDto = that.pageData.records
          let params = that.form
          if (_componentName === 'issued') {
            methodApi = 'addAppraisalPlanDown'
            if (that.form.id) {
              methodApi = 'updateByIdAppraisalPlanDown'
              params.gradeIndicator = that.form.id
            }
          } else if (_componentName === 'distribute') {
            methodApi = 'addAppraisalPlanSeparate'
          } else if (_componentName === 'assign') {
            methodApi = 'downAppraisalPlanAssign'
          }
          Api[methodApi](params)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.error(res.message)
              }
            })
            .finally(() => {
              that.loading = false
              that.handleCancel()
            })
        } else {
          return false
        }
      })
    },

    // 下发
    handleIssued() {
      this.loading = true
      Api.downAppraisalPlanDown([this.checkRow.id])
        .then((res) => {
          if (res.success) {
            this.$message.success('下发成功')
            this.$emit('ok')
            this.handleCancel()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 分发
    handleDistribute() {
      this.loading = true
      Api.downAppraisal({ id: this.checkRow.id })
        .then((res) => {
          if (res.success) {
            this.$message.success('分发成功')
            this.$emit('ok')
            this.handleCancel()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 签收
    handleSign() {
      this.loading = true
      Api.downAppraisalPlanSign({ id: this.checkRow.id })
        .then((res) => {
          if (res.success) {
            this.$message.success('签收成功')
            this.$emit('ok')
            this.handleCancel()
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 提交
    handleSubmit() {
      // this.loading = true
      // Api.downAppraisalPlanAssign({ id: this.checkRow.id })
      //   .then((res) => {
      //     if (res.success) {
      //       this.$message.success('指派成功')
      //       this.$emit('ok')
      //       this.handleCancel()
      //     } else {
      //       this.$message.error(res.message)
      //     }
      //   })
      //   .finally(() => {
      //     this.loading = false
      //   })
    },

    onSelectedRow(list) {
      this.checkList = list
    },

    handleImport() {},

    handleDownload() {},

    // 选择方案回调
    selPlan(_list) {
      this.formByPlan = _list[0]
      this.$set(this.form, 'schemeName', _list[0].schemeName)
      this.getByIdScheme(this.formByPlan.id)
    },

    // 选择下发对象回调
    selObj(_list) {
      _list = _list.map((el) => ({
        ...el,
        tree: this.cloneIndexInfo.tree || [],
        listSjskTableIndicatorDto: this.cloneIndexInfo.listSjskTableIndicatorDto || [],
      }))
      this.$set(this.formByDisObj, 'name', _list.map((el) => el.title).join(','))
      this.$set(this.pageData, 'records', _list)
    },
    // 指标信息
    handleIndexInfo(_row) {
      this.indexInfoCurRow = _row
      if (this.componentName === 'issued') {
        if (!_row.tree.length) {
          this.$message.warn('无指标信息')
          return
        }
      }
      console.log('handleIndexInfo--', _row)
      this.$refs.planIndexInfo.show(_row, this.showType)
    },
    // 指标信息回调
    selIndexInfo(_list) {
      let index = this.pageData.records.findIndex((el) => el.id === this.indexInfoCurRow.id)
      let cloneList = JSON.parse(JSON.stringify(this.pageData.records))
      cloneList[index].listSjskTableIndicatorDto = _list
      this.pageData.records = cloneList
      console.log('this.pageData--', index, this.pageData)
    },

    // 弹出日历和关闭日历的回调
    openChangeOne(status) {
      this.openYear = !this.openYear
    },

    // 得到年份选择器的值
    panelChangeOne(value) {
      this.openYear = false
      this.form.year = this.moment(value).format('YYYY')
    },

    onTimeChange(date) {
      this.$set(this.form, 'startTime', this.moment(date[0]).format('YYYY-MM-DD'))
      this.$set(this.form, 'endTime', this.moment(date[1]).format('YYYY-MM-DD'))
    },

    handleCancel() {
      this.$emit('update:visible', false)
    },
  },
}
</script>

<style lang="less" scoped>
.index-card {
  ::v-deep .ant-card-body {
    padding: 0;
  }
  ::v-deep .ant-card-extra {
    position: absolute;
    left: 135px;
    float: none;
  }
  .index-top {
    padding: 20px;
    border-bottom: 1px solid #ddd;
  }
  .index-con {
    display: flex;
    .index-left {
      width: 300px;
      padding: 20px;
      border-right: 1px solid #ddd;
    }
    .index-right {
      flex: 1;
      padding: 20px;
    }
  }
}
.col-3 {
  width: 100%;
  ::v-deep .ant-form-item-label {
    width: 7%;
  }
  ::v-deep .ant-form-item-control-wrapper {
    width: 67%;
  }
}
.col-2 {
  width: 100%;
  ::v-deep .ant-form-item-label {
    width: 15%;
  }
  ::v-deep .ant-form-item-control-wrapper {
    width: 73%;
  }
}
.footerBox {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
}
.content {
  height: 70vh;
  overflow-y: scroll;
}
::v-deep .ant-card-head {
  background: #eee;
}
::v-deep .ant-card-body {
  padding-bottom: 0;
}
.card-end {
  .table-page-content {
    width: 70%;
    border: 1px solid #eee;
    border-top: none;
  }
  ::v-deep .ant-card-body {
    padding-bottom: 20px;
  }
}
</style>
